package com.xhwl.centiotdata.handler.org.impl;

import com.xhwl.centiotdata.handler.org.OrgFactory;
import com.xhwl.centiotdata.handler.org.OrgHandler;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.IOrgInfoService;
import com.xhwl.centiotdata.suppliersystem.sinoiov.RegionSinoiovHandler;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.pojo.cent.iot.OrgInfo;
import com.xhwl.common.pojo.cent.sdata.Organization;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 中交兴路组织同步
 *
 * @author pengjunming
 * @description
 * @date 2022/11/12 10:13
 */
@Slf4j
@Service
public class OrgSinoiovHandler extends OrgHandler {

    @Resource
    private RegionSinoiovHandler regionSinoiovHandler;

    @Resource
    private IOrgInfoService iOrgInfoService;

    @Override
    public void createOrg(PedestrianIntegrationConfigDTO config, List<Organization> organizations) {
        Organization org = organizations.get(0);
        onAdd(config, org);
    }

    @Override
    public void updateOrg(PedestrianIntegrationConfigDTO config, List<Organization> organizations) {
        Organization org = organizations.get(0);

        OrgInfo orgInfo = iOrgInfoService.getOrgInfo(org.getId(),config.getProductId());
        //如果关系不存在，则新增
        if (Objects.isNull(orgInfo)) {
            onAdd(config, org);
        } else {
            regionSinoiovHandler.update(config, org, orgInfo);
            orgInfo.setOrgName(org.getName());
            iOrgInfoService.updateOrgInfo(orgInfo);
        }
    }

    @Override
    public void deleteOrg(PedestrianIntegrationConfigDTO config, List<Organization> organizations) {
        Integer productId = config.getProductId();
        organizations.forEach(org -> {
            OrgInfo orgInfo = iOrgInfoService.getOrgInfo(org.getId(),productId);
            if (Objects.nonNull(orgInfo) && StringUtils.isNotEmpty(orgInfo.getOrgIndexCode())) {
                boolean flag = regionSinoiovHandler.delete(config, orgInfo.getOrgIndexCode());
                if (flag) {
                    iOrgInfoService.deleteOrgInfo(orgInfo.getId(),productId);
                }
            }
        });

    }

    private void onAdd(PedestrianIntegrationConfigDTO config, Organization org) {
        log.info("开始同步中交兴路组织");
        OrgInfo parent = iOrgInfoService.getOrgInfo(org.getParentId(),config.getProductId());
        log.info("parent Info:" + parent);
        String orgIndexCode = regionSinoiovHandler.add(config, org, parent);
        OrgInfo info = new OrgInfo();
        if (Objects.isNull(parent)) {
            info.setParentIndexCode("1");
        } else {
            info.setParentIndexCode(parent.getOrgIndexCode());
        }
        info.setOrgName(org.getName());
        info.setId(org.getId());
        info.setEnterpriseId(org.getEnterpriseId());
        info.setOrgIndexCode(orgIndexCode);
        info.setParentId(org.getParentId());
        info.setProductId(config.getProductId());
        iOrgInfoService.save(info);
        log.info("结束同步中交兴路组织");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        OrgFactory.register(ProductEnum.SINOIOV_PEDE.getId(), this);
        log.info("中交兴路组织管理注册成功");
    }
}
